package com.pm.service.dept.impl;

import com.pm.entity.dto.dept.DeptTypeScoreDto;
import com.pm.entity.po.caveatConf.CaveatConf;
import com.pm.entity.po.dept.DepartmentScore;
import com.pm.entity.po.dept.EvaluateTaskPo;
import com.pm.entity.vo.ScoreSummaryChildVo;
import com.pm.entity.vo.ScoreSummaryVo;
import com.pm.entity.vo.dept.DepartmentScoreVo;
import com.pm.entity.vo.dept.DeptQuarterScoreVo;
import com.pm.entity.vo.dept.DeptTaskNumVo;
import com.pm.entity.vo.dept.DeptTypeTaskScoreVo;
import com.pm.entity.vo.task.TaskNameVo;
import com.pm.entity.vo.task.TeamScoreRankVo;
import com.pm.mapper.caveatConf.CaveatConfMapper;
import com.pm.mapper.dept.DepartmentScoreMapper;
import com.pm.mapper.task.SubtaskDeptTaskMapper;
import com.pm.service.dept.DepartmentScoreService;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.task.TaskService;
import com.pm.utils.DateUtil;
import com.pm.utils.MergeObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class DepartmentScoreServiceImpl extends BaseServiceImpl<DepartmentScore> implements DepartmentScoreService {
    @Autowired
    DepartmentScoreMapper departmentScoreMapper;
    @Autowired
    SubtaskDeptTaskMapper subtaskDeptTaskMapper;
    @Autowired
    CaveatConfMapper caveatConfMapper;

    @Resource
    TaskService taskService;

    @Resource
    private void setBaseMapper(DepartmentScoreMapper departmentScoreMapper) {
        super.setBaseMapper(departmentScoreMapper);
    }

    @Override
    public List<DepartmentScoreVo> getListDepartmentSocre(DepartmentScore departmentScore) {
        // 重点工作的配合单位数量
        Integer num = subtaskDeptTaskMapper.getNumDepartmentSocre(departmentScore);
        // 查亮灯配置
        CaveatConf caveatConf = caveatConfMapper.getOne(num);
        if (caveatConf != null) {
            // 前几名
            Integer after = caveatConf.getAfter();
            // 后几名
            Integer before = caveatConf.getBefore();

            List<DepartmentScoreVo> deptScores = departmentScoreMapper.getListDepartmentSocre(departmentScore);
            if (deptScores.size() > 1) {
                for (int i = 0; i < deptScores.size(); i++) {
                    // 前几名
                    if (i < after) {
                        deptScores.get(i).setLamp(1);
                    }
                    // 后几名
                    if (i + 1 > (deptScores.size() - before)) {
                        deptScores.get(i).setLamp(0);
                    }
                }
            }
            return deptScores;
        }
        List<DepartmentScoreVo> deptScores = departmentScoreMapper.getListDepartmentSocre(departmentScore);
        return deptScores;
    }

    @Override
    public Integer update1(DepartmentScore departmentScore) {
        return departmentScoreMapper.update1(departmentScore);
    }

    @Override
    public Integer update2(DepartmentScore departmentScore) {
        return departmentScoreMapper.update2(departmentScore);
    }

    @Override
    public void addAll(List<DepartmentScore> departmentScore) {
        if (departmentScore.size() == 0) {
            return;
        }
        // 根据排名排序
        departmentScore.sort(Comparator.comparing(DepartmentScore::getRank));
        // 取集合的第一组数据
        //DepartmentScore departmentScoreOne = departmentScore.get(0);
        // 得到部门数量
        //Integer num = subtaskDeptTaskMapper.getNumDepartmentSocreList(departmentScoreOne);
        Integer num = departmentScore.size();
        // 得到前几后几
        CaveatConf caveatConf = new CaveatConf();
        caveatConf = caveatConfMapper.getOne(num);
        // 添加灯
        if (caveatConf != null) {
            // 前几名
            Integer before = caveatConf.getBefore();
            // 后几名
            Integer after = caveatConf.getAfter();
            if (departmentScore.size() > 1) {
                for (int i = 0; i < departmentScore.size(); i++) {
                    // 前几名
                    if (i < before) {
                        departmentScore.get(i).setCaveat(1);
                    }
                    // 后几名
                    if (i + 1 > (departmentScore.size() - after)) {
                        departmentScore.get(i).setCaveat(0);
                    }
                }
            }
        }
        for (DepartmentScore d :
                departmentScore) {
            if (d.getId() != null) {
                // 如果有删除
                Integer id = d.getId();
                departmentScoreMapper.removeNew(id);
            }
            // 没有、循环添加
            if (d.getScore() != null) {
                d.setType(2);
                d.setYear(DateUtil.getYear(new Date()));
                departmentScoreMapper.addAll(d);
            }
        }
    }

    @Override
    public List<ScoreSummaryVo> getScoreSummary(Integer type) {
        List<ScoreSummaryVo> scoreSummaryVo = null;
        scoreSummaryVo = departmentScoreMapper.getScoreSummary(type);
        for (ScoreSummaryVo scoreSummaryVo1 :
                scoreSummaryVo) {
            List<ScoreSummaryChildVo> children = new ArrayList<>();
            for (ScoreSummaryChildVo scoreSummaryChildVo1 :
                    scoreSummaryVo1.getScoreSummaryChildVoList()) {
                ScoreSummaryChildVo vo = new ScoreSummaryChildVo();
                vo = (ScoreSummaryChildVo) MergeObject.combineSydwCore(scoreSummaryChildVo1, vo);
                if (scoreSummaryVo1.getTaskId() != vo.getDeTaskId()) {
                    vo.setRankOne(null);
                    vo.setCaveatOne(null);
                    vo.setScoreOne(null);
                }
                children.add(vo);
            }
            scoreSummaryVo1.setScoreSummaryChildVoList(children);
        }
        return scoreSummaryVo;
    }

    @Override
    public List<ScoreSummaryVo> getScoreSummaryTask(ScoreSummaryVo scoreSummaryVo) {
        List<ScoreSummaryVo> scoreSummaryVoOne = null;
        scoreSummaryVoOne = departmentScoreMapper.getScoreSummaryTask(scoreSummaryVo);
        return scoreSummaryVoOne;
    }

    @Override
    public Integer removeDepartmentScore(DepartmentScore departmentScore) {
        return departmentScoreMapper.removeDepartmentScore(departmentScore);
    }

    @Override
    public List<EvaluateTaskPo> getScoreByDeptId(DeptTaskNumVo deptTaskNumVo) {
        return departmentScoreMapper.getScoreByDeptId(deptTaskNumVo);
    }

    @Override
    public List<DeptQuarterScoreVo> getQuarterScoreByDeptId(DeptTaskNumVo deptTaskNumVo) {
        // 获取当年有哪些任务
        TeamScoreRankVo teamScoreRankVo = new TeamScoreRankVo();
        teamScoreRankVo.setYear(deptTaskNumVo.getYear());
        List<TaskNameVo> list = subtaskDeptTaskMapper.getDeptTaskByYear(teamScoreRankVo);
        List<DeptQuarterScoreVo> deptQuarterScoreVoList = new ArrayList<>();
        DeptQuarterScoreVo deptQuarterScoreVo = null;
        for (int i = 0, j = list.size(); i < j; i++) {
            if (list.get(i).getTaskTitle() == null) {
                // 跳过本次循环
                continue;
            }
            deptQuarterScoreVo = departmentScoreMapper.getQuarterScoreByDeptId(list.get(i).getId(), deptTaskNumVo.getDeptId());
            if (deptQuarterScoreVo != null) {
                deptQuarterScoreVo.setTaskTitle(list.get(i).getTaskTitle());
                deptQuarterScoreVoList.add(deptQuarterScoreVo);
            }
            /*if (deptQuarterScoreVo == null) {
                deptQuarterScoreVo = new DeptQuarterScoreVo();
            }
            if (deptQuarterScoreVo.getFirstQuarter() == null && deptQuarterScoreVo.getSecondQuarter() == null
                    && deptQuarterScoreVo.getThirdQuarter() == null && deptQuarterScoreVo.getFourQuarter() == null) {
                // 跳过本次循环
                continue;
            }
            deptQuarterScoreVo.setTaskTitle(list.get(i).getTaskTitle());
            deptQuarterScoreVoList.add(deptQuarterScoreVo);*/
        }
        return deptQuarterScoreVoList;
    }

    @Override
    public List<DeptTypeScoreDto> getScoreByDeptTypeAndTaskId(DeptTypeTaskScoreVo deptTypeTaskScoreVo) {
        return departmentScoreMapper.getScoreByDeptTypeAndTaskId(deptTypeTaskScoreVo);
    }
}
